<?php

/**
 * Description of userServiceAction
\* www.teuker.nl - mail@teuker.nl
 * 
 * @author francisc
 */

class userServiceAction extends sfAction {

    public function  execute($request) {
        $xml = "<users>
                  <user>
                     <remark>manager for rest a, access to all functions in rest a</remark>
                     <profitcenter>restaurant a</profitcenter>
                     <usertype>manager</usertype>
                     <userid>6</userid>
                     <username>mana</username>
                     <userpass>40be4e59b9a2a2b5dffb918c0e86b3d7</userpass>
                     <useractive>1</useractive>
                     <first>manager a</first>
                     <last>manager a</last>
                     <email>managera@flavours.nl</email>
                     <mobile>+31631935334</mobile>
                     <userlevel>10</userlevel>
                     <consolemessages>1</consolemessages>
                   </user>
                  <user>
                     <remark>user in restaruant a</remark>
                     <profitcenter>restaurant a</profitcenter>
                     <usertype>user</usertype>
                     <userid>2</userid>
                     <username>user1a</username>
                     <userpass>40be4e59b9a2a2b5dffb918c0e86b3d7</userpass>
                     <useractive>1</useractive>
                     <first>user</first>
                     <last>user</last>
                     <email>user@flavours.nl</email>
                     <mobile></mobile>
                     <userlevel>1</userlevel>
                     <consolemessages>1</consolemessages>
                   </user>
                  <user>
                     <remark>user 2 in rest a</remark>
                     <profitcenter>restaurant a</profitcenter>
                     <usertype>user</usertype>
                     <userid>3</userid>
                     <username>user2a</username>
                     <userpass>40be4e59b9a2a2b5dffb918c0e86b3d7</userpass>
                     <useractive>1</useractive>
                     <first>user2</first>
                     <last>user</last>
                     <email>user2@flavours.nl</email>
                     <mobile></mobile>
                     <userlevel>1</userlevel>
                     <consolemessages>1</consolemessages>
                   </user>
                  <user>
                     <remark>kitchen of restaurant A should only see console messages and reservations from restaurant A</remark>
                     <profitcenter>restaurant a</profitcenter>
                     <usertype>console</usertype>
                     <userid>4</userid>
                     <username>keuk1a</username>
                     <userpass>40be4e59b9a2a2b5dffb918c0e86b3d7</userpass>
                     <useractive>1</useractive>
                     <first>console</first>
                     <last>kitchenA</last>
                     <email>kitchena@flavours.nl</email>
                     <mobile></mobile>
                     <userlevel>1</userlevel>
                     <consolemessages>1</consolemessages>
                   </user>
                  <user>
                     <remark>callcenter employee, access to rest a,b, and c</remark>
                     <profitcenter>restaurant a|restaurant b|restaurant c</profitcenter>
                     <usertype>callcenter</usertype>
                     <userid>8</userid>
                     <username>callabc</username>
                     <userpass>40be4e59b9a2a2b5dffb918c0e86b3d7</userpass>
                     <useractive>1</useractive>
                     <first>callcenterabc</first>
                     <last>callcenter</last>
                     <email>callcenter@flavours.nl</email>
                     <mobile>+31631935334</mobile>
                     <userlevel>99</userlevel>
                     <consolemessages>1</consolemessages>
                   </user>
                  <user>
                     <remark>user of rest b</remark>
                     <profitcenter>restaurant b</profitcenter>
                     <usertype>user</usertype>
                     <userid>9</userid>
                     <username>user1b</username>
                     <userpass>40be4e59b9a2a2b5dffb918c0e86b3d7</userpass>
                     <useractive>1</useractive>
                     <first>user b</first>
                     <last>userb</last>
                     <email>userb@flavours.nl</email>
                     <mobile></mobile>
                     <userlevel>1</userlevel>
                     <consolemessages>1</consolemessages>
                   </user>
                  <user>
                     <remark>kitchen of restaurant b should only see console messages and reservations from restaurant b</remark>
                     <profitcenter>restaurant b</profitcenter>
                     <usertype>console</usertype>
                     <userid>10</userid>
                     <username>keuk1b</username>
                     <userpass>40be4e59b9a2a2b5dffb918c0e86b3d7</userpass>
                     <useractive>1</useractive>
                     <first>console</first>
                     <last>kitchenb</last>
                     <email>kitchenb@flavours.nl</email>
                     <mobile></mobile>
                     <userlevel>1</userlevel>
                     <consolemessages>1</consolemessages>
                   </user>
                  <user>
                     <remark>admin can see all data, edit and change ervything</remark>
                     <profitcenter></profitcenter>
                     <usertype>admin</usertype>
                     <userid>1</userid>
                     <username>admin</username>
                     <userpass>40be4e59b9a2a2b5dffb918c0e86b3d7</userpass>
                     <useractive>admin</useractive>
                     <first>admin</first>
                     <last>for all users</last>
                     <email>admin@flavours.nl</email>
                     <mobile></mobile>
                     <userlevel>99</userlevel>
                     <consolemessages>1</consolemessages>
                   </user>
                  <user>
                     <remark>manager for rest a, b and c access to all functions in rest a, b and c</remark>
                     <profitcenter>restaurant a|restaurant b|restaurant c</profitcenter>
                     <usertype>manager</usertype>
                     <userid>7</userid>
                     <username>manabc</username>
                     <userpass>40be4e59b9a2a2b5dffb918c0e86b3d7</userpass>
                     <useractive>1</useractive>
                     <first>manager abc</first>
                     <last>manager</last>
                     <email>managerabc@flavours.nl</email>
                     <mobile>+31631935334</mobile>
                     <userlevel>10</userlevel>
                     <consolemessages>1</consolemessages>
                   </user>
                  <user>
                     <remark>restaurant console of restaurant A should only see console messages and reservations from restaurant A</remark>
                     <profitcenter>restaurant a</profitcenter>
                     <usertype>console</usertype>
                     <userid>5</userid>
                     <username>cons1a</username>
                     <userpass>40be4e59b9a2a2b5dffb918c0e86b3d7</userpass>
                     <useractive>1</useractive>
                     <first>console</first>
                     <last>restaurant A</last>
                     <email>kitchena@flavours.nl</email>
                     <mobile></mobile>
                     <userlevel>1</userlevel>
                     <consolemessages>1</consolemessages>
                   </user>
                 </users>";
        $request->setParameter('xml', $xml);
        $xml_to_array_obj = new XmlToArray($request->getParameter('xml'));
        $user_array = $xml_to_array_obj->createArray();
        $user_array = $user_array['users']['user'];

        /* update user information */
        foreach ($user_array as $new_user) {
            $user = SqlEngine::getUser(array('id' => $new_user['userid']));

            if (empty($user)) {
                $mobile = '';
                if (isset($new_user['mobile']))
                    $mobile = $new_user['mobile'];

                SqlEngineCore::get()->insert(
                    array('table' => 'user',
                          'columns' => array(
                                'user_id' => array('value' => $new_user['userid'], 'type' => PDO::PARAM_INT),
                                'username' => $new_user['username'],
                                'password' => $new_user['userpass'],
                                'receive_console_messages' => $new_user['consolemessages'],
                                'name_first' => $new_user['first'],
                                'name_last' => $new_user['last'],
                                'email' => $new_user['email'],
                                'mobile' => $mobile,
                                'level' => $new_user['userlevel'],
                                'created_at' => 'NOW()',
                                'remark' => $new_user['remark'],
                                'user_type' => $new_user['usertype'])));

                /* bind the user to each restaurant */
                if ('admin' != $new_user['usertype']) {
                    $user_id = SqlEngineCore::get()->getLastInsertId();
                    $restaurants = explode('|', $new_user['profitcenter']);

                    foreach ($restaurants as $restaurant) {
                        /* if the restaurant is not found in the database, insert it */
                        $result = SqlEngine::getRestaurants(array('name' => $restaurant));
                        if (empty($result)) {
                            SqlEngineCore::get()->insert(array('table' => 'restaurant',
                                                               'columns' => array('name' => $restaurant)));

                            $restaurant_id = SqlEngineCore::get()->getLastInsertId();
                        }
                        else
                            $restaurant_id = $result[0]['restaurant_id'];

                        SqlEngineCore::get()->insert(
                            array('table' => 'user_restaurant',
                                  'columns' => array(
                                        'user_id' => array('value' => $new_user['userid'], 'type' => PDO::PARAM_INT),
                                        'restaurant_id' => array('value' => $restaurant_id, 'type' => PDO::PARAM_INT))));
                    }
                }
            }
        }

        /* bind all restaurants to the admin user */
        $admin_user = SqlEngine::getUser(array('user_type' => 'admin'));
        $restaurants = SqlEngine::getRestaurants();

        foreach ($restaurants as $restaurant) {
            SqlEngineCore::get()->insert(
                array('table' => 'user_restaurant',
                      'columns' => array(
                            'user_id' => array('value' => $admin_user[0]['user_id'], 'type' => PDO::PARAM_INT),
                            'restaurant_id' => array('value' => $restaurant['restaurant_id'], 'type' => PDO::PARAM_INT))));
        }

        return sfView::NONE;
    }
}
